<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pro_data_permintaan_perangkat extends MY_Controller {
	
	// Define Allowed Extension for File Upload
	var $arr_allowed_upload	= array('txt','jpg', 'jpeg', 'png', 'bmp', 'gif','ppt','pptx','xls','xlsx','doc','docx','pdf');
	
	function __construct()
	{
		parent::__construct(); 
		
		// Uses Model
		$this->load->model('pro_data_permintaan_perangkat/sample_master_model', 'Master');
		// $this->load->model('pro_data_permintaan_perangkat/sample_reference_model', 'Ref');
		// $this->load->model('pro_data_permintaan_perangkat/sample_detail2_model', 'Detail2');
		// $this->load->model('pro_data_permintaan_perangkat/sample_detail_file_model', 'DetailFile');
		// $this->load->model('pro_data_permintaan_perangkat/combo_model', 'Combo');
		
		// Secure Page With Session
		$this->secure_session();
	}
	
	function index(){
		// Display Index
		$data['title'] = "Data Permintaan Perangkat";
		// $data['nama_barang']=$this->Master->getApp();
		viewByTemplate('pro_data_permintaan_perangkat/index', $data, 'grid');
	}

	function print_satuan(){
		// Display Form Edit
		$data['title'] = "<b>Data Perangkat</b>";
		$data['data'] = $this->Master->GetSingleView(get('id_permintaan')); 
		$data['data_detail'] = $this->Master->GetJSON_detail(get('id_permintaan'));
		
				
		$data['mode'] = "edit";
		$data['numb_id'] = get('id_permintaan');
		viewByTemplate('pro_data_permintaan_perangkat/print_satuan', $data, 'form');
	}
	
	function getdata()
	{
		// Ajax Response For Datagrid
		echo $this->Master->GetJSON();
	}

	function getdata_detail($id=0)
	{
		// Ajax Response For Datagrid
		echo $this->Master->GetJSON_detail($id);
	}
	
	
	function create()
	{
		// Display Form Create
		$data['title'] = "Tambah Data Permintaan Perangkat";
		$data['data'] = NULL;
		
		$query1 = $this->db->query("SELECT id_permintaan FROM (SELECT * FROM pro_data_permintaan_perangkat ORDER BY id_permintaan DESC) WHERE rownum <=1");
	 				if ($query1->row_array()) {
	 					$getquery1 = $query1->row_array();
	 					$rowcount1 = $getquery1['id_permintaan'];
	 					$autonum1 = $rowcount1 + 1;
	 				} else {
					$autonum1 = 1;
				}
		$data['id_permintaan_auto'] = $autonum1;

		$data['mode'] = "create";
		$data['numb_id'] = $autonum1;
		viewByTemplate('pro_data_permintaan_perangkat/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Data Permintaan Perangkat";
		$data['data'] = $this->Master->GetSingleView(get('id_permintaan'));
		
				
		$data['mode'] = "edit";
		$data['numb_id'] = get('id_permintaan');
		viewByTemplate('pro_data_permintaan_perangkat/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Data Permintaan Perangkat";
		$data['data'] = $this->Master->GetSingleView(get('id_permintaan'));
		
		// Define Combo Data
		$data['arr_reference']	= $this->Combo->SampleReference();
		$data['arr_province']	= $this->Combo->SWProvince();
		
		$data['file_allowed']		= implode(', ', $this->arr_allowed_upload);
		
		$data['mode'] = "view";
		viewByTemplate('pro_data_permintaan_perangkat/form', $data, 'form');
	}
	
	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty		= $this->MessageTemplate("ER001");
		$tpl_duplicate	= $this->MessageTemplate("ER002");


		// Validation
		if (post("id_permintaan") == '') {
			$error->Add("pk_master", "Primary Key", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT COUNT(1) FROM PRO_DATA_PERMINTAAN_PERANGKAT WHERE UPPER(id_permintaan) = '".@strtoupper(@trim(post("id_permintaan")))."'")) > 0) {
			$error->Add("id_permintaan", "Primary Key", $tpl_duplicate);
		}
		
		return $error;
	}

	private function validation_detail()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty		= $this->MessageTemplate("ER001");
		$tpl_duplicate	= $this->MessageTemplate("ER002");


		// Validation
		if (post("id_permintaan_dtl") == '') {
			$error->Add("id_permintaan_dtl", "Primary Key", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT COUNT(1) FROM PRO_DATA_PERMINTAAN_PRK_DTL WHERE UPPER(id_permintaan_dtl) = '".@strtoupper(@trim(post("id_permintaan_dtl")))."'")) > 0) {
			$error->Add("id_permintaan_dtl", "Primary Key", $tpl_duplicate);
		}
		
		return $error;
	}

	function create_detail($id_permintaan)
	{
		// Display Form Create
		$data['title'] = "Tambah Data Perangkat";
		$data['data'] = NULL;

		$query1 = $this->db->query("SELECT id_permintaan_dtl FROM (SELECT * FROM pro_data_permintaan_prk_dtl ORDER BY id_permintaan_dtl DESC) WHERE rownum <=1");
	 				if ($query1->row_array()) {
	 					$getquery1 = $query1->row_array();
	 					$rowcount1 = $getquery1['id_permintaan_dtl'];
	 					$autonum1 = $rowcount1 + 1;
	 				} else {
					$autonum1 = 1;
				}
		
		$data['mode'] = "create_detail";
		$data['numb_id_detail'] = $autonum1;
		viewByTemplate('pro_data_permintaan_perangkat/form_detail', $data, 'form');
	}

	function edit_detail($id_permintaan)
	{
		// Display Form Create
		$data['title'] = "Tambah Data Perangkat";
		$data['data'] = $this->Master->GetSingleView_detail(get('id_permintaan_dtl'));
				
		$data['mode'] = "edit_detail";
		$data['numb_id_detail'] = get('id_permintaan_dtl');
		viewByTemplate('pro_data_permintaan_perangkat/form_detail', $data, 'form');
	}

	function dalate_detail()
	{
		
	}

	function ajax_post_detail($mode="",$id_permintaan=0)
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";
		
		// Get Error Template
		$tpl_insert_success = $this->MessageTemplate("CS001");
		$tpl_update_success = $this->MessageTemplate("CS002");
		
		$error = $this->validation_detail();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data Array

				$data_detail = array('id_permintaan_dtl' => $this->input->post('id_permintaan_dtl')
									,'id_barang' => $this->input->post('id_barangan')
									,'jumlah' => $this->input->post('jumlah')
									,'lokasi' => $this->input->post('lokasi')
									,'keterangan' => $this->input->post('keterangan')
									,'id_permintaan' => $id_permintaan
									);
				//$data_detail['id_barang'] = $data_detail['id_barangan'];
				//$data_detail['id_permintaan'] = $id_permintaan;

				$data = array(
					'id_permintaan' => $id_permintaan
				);

				$cek_data = $this->db->query("select * from PRO_DATA_PERMINTAAN_PERANGKAT WHERE id_permintaan = '$id_permintaan'");
				if ($cek_data->num_rows() == 0) {
					$this->db->insert('PRO_DATA_PERMINTAAN_PERANGKAT',$data);
				}

				unset($data_detail['id_barangan']);

				$pk_master	= @strtoupper(@trim(post("id_permintaan_dtl")));
								
				if ($mode == "create_detail")
				{
					$ls_genid	= $pk_master;
										
					$this->db->insert('PRO_DATA_PERMINTAAN_PRK_DTL',$data_detail);
					
					$ls_response = $tpl_insert_success['message_id'];
				}
				else if ($mode == "edit_detail")
				{
					unset($data['pk_master']);
					unset($data_detail['id_permintaan']);
										
					$this->db->update('PRO_DATA_PERMINTAAN_PRK_DTL',$data_detail, array('id_permintaan_dtl'	=> $pk_master));
					
					$ls_response = $tpl_update_success['message_id'];
				}
				
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}
	
	function ajax_post()
	{
		// Ajax Response For Submit Form
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";
		
		// Get Error Template
		$tpl_insert_success = $this->MessageTemplate("CS001");
		$tpl_update_success = $this->MessageTemplate("CS002");
		
		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data Array

				$data = post_by_key(array('id_permintaan','tanggal','nama','departemen','permohonan_masalah','pilihan_permintaan'));
				
				$pk_master	= post("id_permintaan");

				$arr_tgl_perm = explode('/', $data['tanggal']);
				$tanggal = $arr_tgl_perm[2].'-'.sprintf("%02s",$arr_tgl_perm[1]).'-'.sprintf("%02s",$arr_tgl_perm[0]);
				
				$data['tanggal'] = $tanggal;
								
				if (post("mode") == "create")
				{
					$ls_genid	= $pk_master;
										
					$this->db->insert('PRO_DATA_PERMINTAAN_PERANGKAT',$data);
					
					$ls_response = $tpl_insert_success['message_id'];
				}
				else if (post("mode") == "edit")
				{
					unset($data['pk_master']);
										
					$this->db->update('PRO_DATA_PERMINTAAN_PERANGKAT',$data, array('id_permintaan'	=> $pk_master));
					
					$ls_response = $tpl_update_success['message_id'];
				}
				
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}
	
	function ajax_delete()
	{
		$ls_result	= "";
		
		
		$pk_master	= get('id_permintaan');
		if ($pk_master != "")
		{
				// Delete Single
			$arr_rule	= array('id_permintaan' => $pk_master);
			$ls_result = $this->Master->DeleteSingle($arr_rule);
		}
		else
		{
			$num_row = intval($this->data['num_row']['deleted']);

			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];

				$arr_rule	= array('id_permintaan' => $row['id_permintaan']);
				$ls_result .= $this->Master->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}

	function ajax_delete_detail($id_permintaan=0)
	{
		$ls_result	= "";
		
		
		$pk_master	= get('id_permintaan_dtl');
		if ($pk_master != "")
		{
				// Delete Single
			$arr_rule	= array('id_permintaan_dtl' => $pk_master);
			$ls_result = $this->Master->DeleteSingleDetail($arr_rule);
		}
		else
		{
			$num_row = intval($this->data['num_row']['deleted']);

			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];

				$arr_rule	= array('id_permintaan_dtl' => $row['id_permintaan_dtl']);
				$ls_result .= $this->Master->DeleteSingleDetail($arr_rule);
			}
		}
		
		echo "<script>
			loadPage(gs_path + '/' + controller + '/edit/?id_permintaan='+urlencode(".$id_permintaan."));
		</script>
		";
	}
}
?>